package com.mvp.shq.washer.ui.shop.order.orderdesc;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jaeger.library.StatusBarUtil;
import com.mvp.shq.washer.R;
import com.mvp.shq.washer.adapter.OrderListAdapter;
import com.mvp.shq.washer.adapter.OrderPayListAdapter;
import com.mvp.shq.washer.base.BaseActivity;
import com.mvp.shq.washer.entity.shop.result.OrderDataListResult;
import com.mvp.shq.washer.entity.shop.result.OrderDetailListRestult;
import com.mvp.shq.washer.rxbus.RxBus;
import com.mvp.shq.washer.rxbus.Subscribe;
import com.mvp.shq.washer.utils.DateUtils;
import com.mvp.shq.washer.utils.EventCode;
import com.mvp.shq.washer.utils.JumpUtil;
import com.mvp.shq.washer.utils.RecyclerViewDivider;
import com.mvp.shq.washer.utils.StringUtils;
import com.orhanobut.logger.Logger;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by westing on 2017/10/11.
 */
public class OrderDescActivity extends BaseActivity<Presenter, Model> implements OrderDescConstract.View {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.address_person)
    TextView addressPerson;//收件人
    @BindView(R.id.address_phone)
    TextView addressPhone;//收件电话
    @BindView(R.id.tv_address)
    TextView tvAddress;//收件地址
    @BindView(R.id.lin_address)
    LinearLayout linAddress;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.order_money1)
    TextView orderMoney1;//商品总价金额
    @BindView(R.id.order_add1)
    TextView orderAdd1;
    @BindView(R.id.order_ji1)
    TextView orderJi1;//商品总价积分
    @BindView(R.id.order_sendmoney)
    TextView orderSendmoney;//配送费
    @BindView(R.id.tv_money_1)
    TextView tvMoney1;//需付款:元
    @BindView(R.id.tv_money2)
    TextView tvMoney2;//需付款:角分
    @BindView(R.id.ll_money)
    LinearLayout llMoney;
    @BindView(R.id.tv_add)
    TextView tvAdd;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;//需付款积分
    @BindView(R.id.ll_integral)
    LinearLayout llIntegral;
    @BindView(R.id.orderid)
    TextView orderid;//订单ID
    @BindView(R.id.orderpayid)
    TextView orderpayid;//支付ID
    @BindView(R.id.order_createtime)
    TextView orderCreatetime;//创建ID
    @BindView(R.id.order_paytime)
    TextView orderPaytime;//支付时间
    @BindView(R.id.order_endtime)
    TextView orderEndtime;//收货时间
    @BindView(R.id.bnt_evaluate)
    TextView bntEvaluate;//评价
    @BindView(R.id.bnt_repay)
    TextView bntRepay;//退款
    private double cashPrice=0;
    private double integralPrice=0,totalPrice=0;
    private String orderId;
    @Override
    public int getLayoutId() {
        return R.layout.activity_orderdesc;
    }

    @Override
    public void initView() {
        Intent intent=getIntent();
        if(intent!=null){
            orderId=intent.getStringExtra("orderId");
            presenter.getOrderDetail(orderId);
        }
        StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.money), 9);
        title.setText("订单详情");
        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        RecyclerViewDivider divider = new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, 10, ContextCompat.getColor(context, R.color.white));
        recyclerview.addItemDecoration(divider);
        OrderListAdapter listadapter = new OrderListAdapter(context,null);
        recyclerview.setAdapter(listadapter);

    }

    @OnClick({R.id.back, R.id.bnt_evaluate,R.id.bnt_repay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.bnt_evaluate:
                //评论
                JumpUtil.toOrderProductComment(context);
                break;
            case R.id.bnt_repay:
                //退款
                JumpUtil.toRefund(context,orderId,cashPrice,integralPrice,totalPrice);
                break;
        }
    }

    @Override
    public void getOrderDetail(OrderDataListResult orderDataListResult) {
        if(orderDataListResult==null)return;
        result = orderDataListResult;
        addressPerson.setText(orderDataListResult.getConsigneeName());//收件人
        addressPhone.setText(orderDataListResult.getConsigneeMobile());//收件电话
        tvAddress.setText(orderDataListResult.getAddressDes());//收件地址
        List<OrderDetailListRestult> list=orderDataListResult.getEcOrderDetailList();
        for (int i=0;i<list.size();i++){
            OrderDetailListRestult orderDetailListRestult=list.get(i);
            cashPrice += orderDetailListRestult.getCashPrice();
            integralPrice += orderDetailListRestult.getIntegralPrice();
        }
        totalPrice=cashPrice;
        if(integralPrice==0)
        {
            llIntegral.setVisibility(View.GONE);
            orderAdd1.setVisibility(View.GONE);
            orderJi1.setVisibility(View.GONE);
        }else if(cashPrice==0){
            llMoney.setVisibility(View.GONE);
            orderAdd1.setVisibility(View.GONE);
            orderMoney1.setVisibility(View.GONE);

        }
        tvMoney1.setText((int)cashPrice+"");
        orderMoney1.setText((int)cashPrice+"");
        tvMoney2.setText(StringUtils.BigDecimalToStr(BigDecimal.valueOf(cashPrice)));
        tvIntegral.setText((int)integralPrice+"");
        orderJi1.setText((int)integralPrice+"");
        orderid.setText("订单编号："+orderDataListResult.getOrderNo());
        orderCreatetime.setText("创建时间："+orderDataListResult.getCreateDate());
        orderPaytime.setText("支付时间："+orderDataListResult.getPayTime());
        orderEndtime.setText("收货时间："+orderDataListResult.getReceiveTime());
        OrderPayListAdapter listadapter = new OrderPayListAdapter(context,orderDataListResult.getEcOrderDetailList());
        recyclerview.setAdapter(listadapter);

    }

    private OrderDataListResult result;
    @Subscribe(code = EventCode.SHOP_REFUND_SEND_ORDERINFO)
    public void sendOrderInfo(){
        Logger.d("发送数据");
        RxBus.get().send(EventCode.SHOP_REFUND_GET_ORDERINFO,result);
    }
}
